/**
 *
 */

package it.bd.SetAllConnectionsOff.activity;

import android.content.Context;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TabHost;
import android.widget.Toast;

import it.bd.SetAllConnectionsOff.R;
import it.bd.SetAllConnectionsOff.helper.BluetoothHelper;
import it.bd.SetAllConnectionsOff.helper.GpsHelper;
import it.bd.SetAllConnectionsOff.helper.NetworkHelper;
import it.bd.SetAllConnectionsOff.helper.PowerHelper;
import it.bd.SetAllConnectionsOff.helper.WifiHelper;

/**
 * tab that chages the settings immediately
 * 
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class CurrentStateActivity extends ATabActivity {

    // private Context context;

    private Context context;

    public CurrentStateActivity() {
        super();
    }

    public CurrentStateActivity(TabHost tabHost, Context context) {
        super();
        this.context = context;
        attachOnTabHost(tabHost);
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.t6.ATabActivity#getIndicator()
     */
    @Override
    String getIndicator() {
        return getContext().getString(R.string.currentSettings);
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.t6.ATabActivity#getTabTag()
     */
    @Override
    String getTabTag() {
        return "currentState";
    }

    @Override
    void setupWifi() {
        cbWifiState.setChecked(false);
        final WifiHelper wh = WifiHelper.getInstance();
        cbWifiState.setEnabled(wh.isServicePresent());
        if (cbWifiState.isEnabled()) {
            cbWifiState.setChecked(wh.isServiceEnabled());
            cbWifiState.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(getContext(), isChecked ? R.string.msgWifiOn : R.string.msgWifiOff, Toast.LENGTH_SHORT).show();
                    wh.setServiceEnabled(isChecked);
                }
            });

        }
    }

    @Override
    void setupBluetooth() {
        CheckBox c = cbBluetoothState;
        final BluetoothHelper helper = BluetoothHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            c.setChecked(helper.isServiceEnabled());
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(getContext(), isChecked ? R.string.msgBtOn : R.string.msgBtOff, Toast.LENGTH_SHORT).show();
                    helper.setServiceEnabled(isChecked);
                }
            });
        }
    }

    @Override
    void setupGps() {
        CheckBox c = cbGpsState;
        final GpsHelper helper = GpsHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            c.setChecked(helper.isServiceEnabled());
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(getContext(), isChecked ? R.string.msgGpsOn : R.string.msgGpsOff, Toast.LENGTH_SHORT).show();
                    helper.setServiceEnabled(isChecked);
                }
            });
        }
    }

    @Override
    void setupScreenBrightness() {
        boolean isAutomatic = PowerHelper.getInstance().isBrightnessAutomatic();
        cbBrightness.setChecked(!isAutomatic);
        sbScreenBrightness.setEnabled(!isAutomatic);
        if (!isAutomatic) {
            int currBrightness = PowerHelper.getInstance().getScreenBrighteness();
            sbScreenBrightness.setProgress(currBrightness);
            tvBrightness.setText("" + currBrightness);
        } else {
            sbScreenBrightness.setProgress(0);
            tvBrightness.setText("");
        }

        cbBrightness.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getContext(), isChecked ? R.string.msgBrightnessManual : R.string.msgBrightnessAutomatic, Toast.LENGTH_SHORT)
                        .show();

                PowerHelper.getInstance().setBrightnessAutomatic(!isChecked);
                sbScreenBrightness.setEnabled(isChecked);
                if (isChecked) {
                    int currBrightness = PowerHelper.getInstance().getScreenBrighteness();
                    sbScreenBrightness.setProgress(currBrightness);
                    tvBrightness.setText(" " + currBrightness);
                } else {
                    sbScreenBrightness.setProgress(0);
                    tvBrightness.setText("");
                }

            }
        });
        sbScreenBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    PowerHelper.getInstance().setScreenBrighteness(progress);
                    tvBrightness.setText("" + progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    @Override
    void setupNetwork() {
        CheckBox c = cbNetworkState;
        final NetworkHelper helper = NetworkHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            c.setChecked(helper.isServiceEnabled());
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Toast.makeText(getContext(), isChecked ? R.string.msgNetworkOn : R.string.msgNetworkOff, Toast.LENGTH_SHORT).show();
                    helper.setServiceEnabled(isChecked);
                }
            });
        }
    }

    @Override
    Context getContext() {
        if (context == null) {
            Log.w("", "Context is null");
            context = getApplicationContext();
        }
        return context;
    }
}
